package com.raven.craft.code.generator.mysql.schema;

import com.raven.craft.code.generator.mysql.repo.entity.Column;
import com.raven.craft.code.generator.mysql.repo.entity.Table;
import lombok.*;

import java.lang.reflect.Field;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Section {

    private String name;
    private String desc;
    private List<List<String>> rows;

    public static Section getSection(List<String> schemaTitles, Table table) {
        List<List<String>> rows = table.getColumns().stream()
                .map(field -> schemaTitles.stream().map(tableTitle -> {
                            try {
                                Field f = Column.class.getDeclaredField(tableTitle);
                                f.setAccessible(true);
                                return String.valueOf(f.get(field));
                            } catch (NoSuchFieldException e) {
                                return "No Such Column";
                            } catch (IllegalAccessException e) {
                                return "Unaccessable";
                            }
                        }).collect(Collectors.toList())

                ).collect(Collectors.toList());

        return new Section(table.getName(), table.getStatus().getComment(), rows);
    }

}